package com.example.bbgds.test;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewConfigurationCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bbgds on 2016/3/1.
 */
public class VoiceLayout extends RelativeLayout {

    private HistoryView mHistoryView;
    private EventController mEventController;
    private RelativeLayout mView1;
    private int mTouchSlop;
    private int mTouchSlopSquared;

    public VoiceLayout(Context context) {
        this(context, null);
    }

    public VoiceLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VoiceLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onFinishInflate() {
        super.onFinishInflate();
        intiView();
    }

    private void intiView() {
        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(ViewConfiguration.get(getContext()));
        mTouchSlopSquared = mTouchSlop * mTouchSlop;
        mHistoryView = (HistoryView) findViewById(R.id.historyview);
//        mView1 = (RelativeLayout)findViewById(R.id.view1);
        mEventController = new EventController(this, null);
        mEventController.addDropTarget(mHistoryView);
        mHistoryView.setExpandListener(new ExpandView.ExpandListener() {
            @Override
            public void onSlide(View expandView, float slideOffset) {
                Log.e("zhu","slideOffset=" + slideOffset);
            }

            @Override
            public void onExpaned(View expandView) {

            }

            @Override
            public void onCollapsed(View expandView) {

            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        Log.e("zhu","mHistoryView.isExpand()=" + mHistoryView.isExpand());
        if(!mHistoryView.isExpand()){
            mEventController.onTouchEvent(ev);
        }
        return true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.e("zhu","mHistoryView.isExpand()=" + mHistoryView.isExpand());
        if(!mHistoryView.isExpand()){
            if(mEventController.onTouchEvent(ev)){
                return true;
            }
        }
        return super.onInterceptTouchEvent(ev);
    }

//    boolean ishandld = false;
//    private float mLastMotionX;
//    private float mLastMotionY;
//    private boolean isMoveMode = false;
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        if(!mHistoryView.isExpand()){
//            return super.dispatchTouchEvent(ev);
//        }
//        final int action = ev.getAction();
//        switch (action) {
//            case MotionEvent.ACTION_DOWN:
//                ishandld = false;
//                isMoveMode = false;
//                mLastMotionX = (int) ev.getRawX();
//                mLastMotionY = (int) ev.getRawY();
//                if(mView1.dispatchTouchEvent(ev)){
//                    ishandld = true;
//                    mHistoryView.dispatchTouchEvent(ev);
//                }else{
//                    return super.dispatchTouchEvent(ev);
//                }
//                break;
//            case MotionEvent.ACTION_MOVE:
////                Log.e("zhu" , "move=" + isMoveMode);
//                if(!ishandld){
//                    return super.dispatchTouchEvent(ev);
//                }else if(isMoveMode){
//                     mHistoryView.dispatchTouchEvent(ev);
//                }else{
//                    float deltaY = ev.getRawY() - mLastMotionY;
//                    float x = ev.getX();
//                    float y = ev.getY();
////                    Log.e("zhu", Math.abs(deltaY) +"=" + mTouchSlop);
//                    if(Math.abs(deltaY) > mTouchSlop){
//                        //if(EventController.canScroll(mHistoryView, true, deltaY, x, y) || mHistoryView.onInterceptTouchEvent(ev)){
//                            isMoveMode = true;
//                            mHistoryView.dispatchTouchEvent(ev);
//                        //}
//                    }else{
//                        super.dispatchTouchEvent(ev);
//                    }
//                }
//                break;
//            case MotionEvent.ACTION_CANCEL:
//            case MotionEvent.ACTION_UP:
////                Log.e("zhu", isMoveMode +"=" + ishandld);
//                if(isMoveMode){
//                    mHistoryView.dispatchTouchEvent(ev);
//                }else{
//                    if(ishandld){
//                        mView1.dispatchTouchEvent(ev);
//                    }else{
//                        super.dispatchTouchEvent(ev);
//                    }
//                }
//                ishandld = false;
//                mLastMotionX = 0;
//                mLastMotionY = 0;
//                isMoveMode = false;
//                break;
//        }
//        return true;
//    }

}
